package bairui.international.weixin.portal.miniapp.controller.barter.order;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_ProductOrder;
import bairui.international.model.status.State_ProductOrder;
import bairui.international.model.type.Type_ProductOrder;
import bairui.international.service.Service_ProductOrder;
import bairui.international.service.Service_ProductOrderItem;
import bairui.international.weixin.portal.miniapp.controller.product.product.C_Product_ShowImg_Get;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 列表
 *
 * @generated
 * 
 * @see STest_C_Barter_Order_Page_Get 测试
 *
 */
@Controller
public class C_Barter_Order_Page_Get
 extends SC_Barter_Order_Page_Get 
{
	
	@Autowired private Service_ProductOrder productOrder;
	@Autowired private Service_ProductOrderItem productOrderItem;
//	@Autowired private Service_ProductOrderGrade service_ProductOrderGrade;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam Integer startPage,
		@RequestParam String skey
	) throws Exception {
		Page<TB_ProductOrder> page = productOrder.page(
				it->it.user.skey.eq(skey)
					.and(it.type.eq(Type_ProductOrder.Grade)),
				PageRequest.of(startPage, 5),
				it->new QSort(it.createTime.desc())
			);
		return page.map(it->new Output(
				it.getId(), 
				it.getNumber(), 
				it.getState(),
				it.getState().getComment(),
				it.getScore(), 
				it.getComment(),
				productOrderItem
					.list(it2->it2.productOrder.eq(it))
					.stream()
					.map(it2->new ItemOutput(
							it2.getId().toString(),
							it2.getProductName(), 
							it2.getProductNumber(), 
							it2.getProductScore(),
//							new C_Barter_Order_ShowImg_Get.Get.Operate() {
//								@Override public Object getId() {
//									return it2.getId();
//								}
//							}.getUrl()
							new C_Product_ShowImg_Get.Get.Operate() {
								@Override public Object getId() {
									return it2.getProduct().getId();
								}
							}.getUrl()
						))
					.collect(Collectors.toList())
			));
	}
	
	@AllArgsConstructor
	public static class Output {
		@Getter private long id;
		@Getter private String number;
		@Getter private State_ProductOrder state;
		@Getter private String stateComment;
		@Getter private Double totalScore;
		@Getter private String comment;
		@Getter private List<ItemOutput> items;
		
	}

	@AllArgsConstructor
	public static class ItemOutput {
		@Getter private String id;
		@Getter private String name;
		@Getter private String number;
		@Getter private Double score;
		@Getter private String imgUrl;
	}
	
}
